Title: Mastering Truffle: A Comprehensive Guide for Smart Contract Development
Subtitle: Including JavaScript Basics
let name = "Alice"; // String
let age = 30; // Number
let isDeveloper = true; // Boolean
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice"));
let person = { name: "Alice", age: 30 };
let numbers = [1, 2, 3, 4];
console.log(person.name); // Output: Alice
console.log(numbers[0]); // Output: 1
let age = 30;
if (age > 18) {
console.log("Adult");
} else {
console.log("Minor");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
Explanation:
Node.js is a runtime for executing JavaScript outside the browser, commonly used for backend and blockchain development.
Code Example:
node --version
Definition:
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine, allowing developers to run JavaScript on the server side.
Why is Node.js important?:
Key Features:
Common Uses:
Single-Threaded Event Loop:
Non-Blocking I/O:
Event-Driven Architecture:
Code Example:
const http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello, World!');
res.end();
});
server.listen(3000, () => {
console.log('Server is listening on port 3000');
});
Blocking vs Non-Blocking:
Concurrency:
Code Comparison:
const fs = require('fs');
const data = fs.readFileSync('file.txt', 'utf8'); // Blocks execution
console.log(data);
fs.readFile('file.txt', 'utf8', (err, data) => {
console.log(data); // Asynchronous, doesn't block execution
});
What are Modules?:
Node.js uses modules to organize and reuse code. Modules can be built-in (core modules like fs, http) or custom.
Common Core Modules:
http: Used to create servers and handle HTTP requests.fs: Used for file system operations like reading and writing files.path: Provides utilities for working with file and directory paths.Using Modules:
require() function.Code Example:
const fs = require('fs');
// Read a file asynchronously
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Custom Modules:
You can create your own modules by exporting code from one file and importing it into another.
Example:
// greet.js (custom module)
module.exports = function greet(name) {
return `Hello, ${name}!`;
};
// app.js
const greet = require('./greet');
console.log(greet('Alice')); // Output: Hello, Alice!
What is npm?:
Key Features:
Common npm Commands:
npm install <package-name>: Installs a package locally in your project.npm install -g <package-name>: Installs a package globally.npm init: Initializes a new project with package.json.Code Example:
# Initialize a new project
npm init
# Install a package locally
npm install express
# Example: Using Express in Node.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
node -v
npm -v
npm install -g truffle
truffle init to create a new project.truffle init
pragma solidity ^0.8.0;
contract HelloWorld {
string public greeting = "Hello, World!";
}
truffle compile
[
{
"constant": false,
"inputs": [],
"name": "greet",
"outputs": [{ "name": "", "type": "string" }],
"type": "function"
}
]
Here’s a detailed slide explaining the truffle migrate command, along with an example:
Purpose:
truffle migrate command is used to deploy smart contracts to a specific network (e.g., local blockchain, testnet, or mainnet).Default Behavior:
migrations directory in sequential order.truffle-config.js.Migration Scripts:
Sample Migration Script (2_deploy_contracts.js):
const HelloWorld = artifacts.require("HelloWorld");
module.exports = function(deployer) {
// Deploy the HelloWorld contract
deployer.deploy(HelloWorld);
};
Explanation:
HelloWorld contract to the blockchain.truffle migrateCommand:
Run the migration command to deploy contracts.
truffle migrate
Output:
Setup:
Make sure Ganache is running on port 8545.
truffle-config.js should contain the local development network configuration:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*", // Match any network id
}
}
};
Run Migration:
truffle migrate --network development
Output:
Compiling your contracts...
===========================
> Compiling ./contracts/HelloWorld.sol
Starting migrations...
======================
> Network name: 'development'
> Network id: 5777
> Block gas limit: 6721975
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x...
> Blocks: 0 Seconds: 0
> contract address: 0x...
> block number: 1 block timestamp: ...
> account: 0x...
> gas used: 261495
> gas price: 20 gwei
2_deploy_contracts.js
=====================
Deploying 'HelloWorld'
----------------------
> transaction hash: 0x...
> contract address: 0x...
> gas used: 181234
> gas price: 20 gwei
Summary
=======
> Total deployments: 2
Result:
truffle migrate --network development
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
}
}
};
truffle migrate --network ropsten